import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '@/views/Login/Login'// 引入登录页面组件
// import { component } from 'vue/types/umd'
import LayoutView from '@/views/Layout/Layout'
import Page404 from '../views/404/404.vue'
import HomeView from '@/views/Home/Home'
import ArticleAdd from '@/views/Article/ArticleAdd'

Vue.use(VueRouter)
// 路由配置表
const routes = [
  {
    path: '/',
    redirect: '/layout'
  }, { // 配置登录页路由
    path: '/login',
    name: 'Login',
    component: LoginView // 对应的组件
  }, {
    path: '/layout', // 首页一级路由
    name: 'Layout',
    component: LayoutView,
    redirect: '/Layout/home', // 重定向home页
    children: [{
      path: 'home', // home 页
      name: 'home',
      component: HomeView
    }, {
      path: 'articleAdd', // 发布文章页
      name: 'articleAdd',
      component: ArticleAdd
    }, {

      path: 'articleList', // 内容管理页
      name: 'articleList',
      component: () => import('@/views/Article/ArticleList')
    },
    {
      path: 'material', // 内容管理页
      name: 'material',
      component: () => import('@/views/Material/Material')
    },
    {
      path: 'comment', // 评论管理
      name: 'comment',
      component: () => import('@/views/Comment/Comment')
    },
    { // 个人设置
      path: 'settings',
      name: 'settings',
      component: () => import('@/views/Settings/Settings')
    },
    { // 粉丝路由
      path: 'fans',
      name: 'fans',
      component: () => import('@/views/Fans/Fans')
    }
    ]

  },
  {
    path: '*',
    name: '404',
    component: Page404
  }

]

const router = new VueRouter({
  routes
})
// 路由守为
router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !sessionStorage.getItem('token')) {
    next('/login')
  } else {
    next()
  }
})

export default router
